home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 016 / source.files / unpacker.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  2KB  |  60 lines

  1. /*----------------------------------------------------------------------*
  2.  * unpacker.c Convert data from "cmpByteRun1" run compression. 11/15/85
  3.  *
  4.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  5.  * This software is in the public domain.
  6.  *
  7.  *    control bytes:
  8.  *     [0..127]   : followed by n+1 bytes of data.
  9.  *     [-1..-127] : followed by byte to be repeated (-n)+1 times.
  10.  *     -128       : NOOP.
  11.  *
  12.  * This version for the Commodore-Amiga computer.
  13.  *----------------------------------------------------------------------*/
  14. #include "iff/packer.h"
  15.  
  16.  
  17. /*----------- UnPackRow ------------------------------------------------*/
  18.  
  19. #define UGetByte()    (*source++)
  20. #define UPutByte(c)    (*dest++ = (c))
  21.  
  22. /* Given POINTERS to POINTER variables, unpacks one row, updating the source
  23.  * and destination pointers until it produces dstBytes bytes. */
  24. BOOL UnPackRow(pSource, pDest, srcBytes0, dstBytes0)
  25.     BYTE **pSource, **pDest;  WORD srcBytes0, dstBytes0; {
  26.     register BYTE *source = *pSource;
  27.     register BYTE *dest   = *pDest;
  28.     register WORD n;
  29.     register BYTE c;
  30.     register WORD srcBytes = srcBytes0, dstBytes = dstBytes0;
  31.     BOOL error = TRUE;    /* assume error until we make it through the loop */
  32.     WORD minus128 = -128;  /* get the compiler to generate a CMP.W */
  33.  
  34.     while( dstBytes > 0 )  {
  35.     if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  36.         n = UGetByte();
  37.  
  38.         if (n >= 0) {
  39.         n += 1;
  40.         if ( (srcBytes -= n) < 0 )  goto ErrorExit;
  41.         if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  42.         do {  UPutByte(UGetByte());  } while (--n > 0);
  43.         }
  44.  
  45.         else if (n != minus128) {
  46.         n = -n + 1;
  47.         if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  48.         if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  49.         c = UGetByte();
  50.         do {  UPutByte(c);  } while (--n > 0);
  51.         }
  52.     }
  53.     error = FALSE;    /* success! */
  54.  
  55.   ErrorExit:
  56.     *pSource = source;  *pDest = dest;
  57.     return(error);
  58.     }
  59.  
  60.